<template>
  <Row>
    <i-col>
      <!-- 静态table，内置静态搜索，动态修改，表的值 2018/7/15 zhangQ -->
      <slot>
      </slot>
    </i-col>
  </Row>
</template>

<script>
  import _ from 'lodash'

  export default {
    name: 'staticTable',
    data () {
      let datas = {
        slotTable: {},
        oldObjData: []
      }
      return datas
    },
    props: {
      /**
       * 过滤函数，每行都执行
       * @param row:::行对象，index:::索引，filterValue:::过滤值
       * @return Boolean
       * @auther zhangQ
       * @date 2018/7/16 9:34
       */
      filterMethod: {
        type: Function,
        default () {
          return null
        }
      },
      filterValue: {
        type: String,
        default () {
          return ''
        }
      }
    },
    computed: {},
    watch: {
      // filterValue (val, oldVal) {
      //   if (val) {
      //     this.customFilter()
      //   } else {
      //     // this.cancelCustomFilter()
      //   }
      // }
    },
    methods: {
      /**
       * 获得选中的数据，编辑过的数据
       * @auther zhangQ
       * @date 2018/7/16 14:45
       */
      getCheckedData () {
        let resData = []
        _.forIn(this.slotTable.objData, (val, key) => {
          let rebuildDataItem = this.slotTable.rebuildData[key]
          if (!rebuildDataItem) {
            rebuildDataItem = {}
          }
          let itemData = Object.assign(val, rebuildDataItem)
          if (itemData._isChecked) {
            // delete itemData._isChecked
            // delete itemData._isDisabled
            // delete itemData._isExpanded
            // delete itemData._isHighlight
            // delete itemData._isHover
            resData.push(itemData)
          }
        })
        return resData
      },
      /**
       * 自定义过滤方法
       * @auther zhangQ
       * @date 2018/7/16 9:30
       */
      customFilter () {
        if (typeof this.filterMethod !== 'function' && this.slotTable.objData) {
          return
        }
        let resData = []
        _.forIn(this.slotTable.objData, (val, key) => {
          let row = this.slotTable.rebuildData[key]
          if (!row) {
            row = {}
          }
          let rebuildDataItem = Object.assign(val, row)
          if (val._isChecked || this.filterMethod(rebuildDataItem, key)) {
            val = rebuildDataItem
            resData.push(val)
          }
        })
        this.slotTable.rebuildData = resData
      },
      /**
       * 取消自定义过滤
       * @auther zhangQ
       * @date 2018/7/16 9:40
       */
      cancelCustomFilter () {
        // let resData = []
        // _.forIn(this.slotTable.objData, (val, key) => {
        //   resData.push(val)
        // })
        let oldObjData = _.cloneDeep(this.slotTable.objData)
        this.slotTable.handleFilterReset(1)
        _.forIn(oldObjData, (val, key) => {
          this.slotTable.objData[key] = val
          let itemData = Object.assign(this.slotTable.rebuildData[key], val)
          delete itemData._isChecked
          delete itemData._isDisabled
          delete itemData._isExpanded
          delete itemData._isHighlight
          delete itemData._isHover
          this.slotTable.rebuildData[key] = itemData
        })
      },
      /**
       * TODO(得到slot的值)
       * @auther zhangQ
       * @date 2018/7/15 22:53
       */
      setTableData () {
        // 获得table的值
        // console.log('this.$slots.default')
        console.log(this.slotTable)
      }
    },
    created () {
    },
    mounted () {
      this.slotTable = this.$slots.default[0].componentInstance
    },
    components: {}
  }
</script>

<style scoped>

</style>